package xps.and.uudaijia.userclient.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import rx.Subscriber;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.InformationBean;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.AppCommond;
import xps.and.uudaijia.userclient.util.CommonUtils;
import xps.and.uudaijia.userclient.view.MainActivity;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by XPS on 2017/6/24.
 */

public class PersonActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.tv_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.update_layout)
    AutoRelativeLayout updateLayout;
    @BindView(R.id.SetUp_Layout)
    AutoRelativeLayout SetUpLayout;
    @BindView(R.id.Sign_in)
    Button SignIn;
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    private static final int RESULT_LOAD_IMAGE = 1;
    @BindView(R.id.ar_account)
    AutoRelativeLayout arAccount;
    String User_Phone = "";
    String name = "";
    String User_Img = "";
    @BindView(R.id.YE_Text)
    TextView YEText;
    @BindView(R.id.YE_Layout)
    AutoRelativeLayout YELayout;
    @BindView(R.id.XFMX_Layout)
    AutoRelativeLayout XFMXLayout;
    @BindView(R.id.ar_discount)
    AutoRelativeLayout arDiscount;
    @BindView(R.id.JFSC_Layout)
    AutoRelativeLayout JFSCLayout;
    @BindView(R.id.banben_Text)
    TextView banbenText;

    @Override
    protected int getLayout() {
        return R.layout.activity_person;
    }

    @Override
    protected void setData() {
        actionbarTvTitle.setText("个人中心");
        getInformation();
        banbenText.setText("V"+JUtils.getAppVersionName());

    }

    //图片标记
    int successPicCount = 0;//已经成功的个数
    int actualPicCount = 0;//应该成功的个数

    @Override
    public void photoUploadSuccess() {
        successPicCount++;
        dissmisDialog();
    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.XFMX_Layout, R.id.JFSC_Layout, R.id.ar_discount, R.id.YE_Layout, R.id.ar_order, R.id.update_layout, R.id.SetUp_Layout, R.id.Sign_in, R.id.iv_header, R.id.ar_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                PersonActivity.this.finish();
                break;
            case R.id.ar_order:
                CommonUtils.AppStartActivity(this, MyOrderActivity.class);
                break;
            case R.id.update_layout:
                Intent intent = new Intent(PersonActivity.this, UpDTPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.SetUp_Layout:
                Intent SetUpintent = new Intent(PersonActivity.this, SetUpActivity.class);
                startActivity(SetUpintent);
                break;
            case R.id.Sign_in:
                SharedPreferences sp = JUtils.getSharedPreference();
                SharedPreferences.Editor spEd = sp.edit();
                spEd.putString("User_Access_token", "");
                spEd.putString("User_ID", "");
                spEd.putString("User_Name", "");
                spEd.putString("User_Phone", "");
                spEd.putString("User_Img", "");
                spEd.putBoolean("User_Login", false);
                spEd.commit();
                Intent Logintent = new Intent(PersonActivity.this, LoginActivity.class);
                startActivity(Logintent);
                finish();
                JPushInterface.setAliasAndTags(PersonActivity.this, "", null, mAliasCallback);
                break;
            case R.id.iv_header:
                Intent ModifyMeintent = new Intent(PersonActivity.this, ModifyMeActivity.class);
                startActivity(ModifyMeintent);
                break;
            case R.id.ar_account:
                Intent Refintent = new Intent(PersonActivity.this, RechargeActivity.class);
                startActivity(Refintent);
                break;
            case R.id.YE_Layout:
                Intent Refintent1 = new Intent(PersonActivity.this, RechargeActivity.class);
                startActivity(Refintent1);
                break;
            case R.id.XFMX_Layout:
                Intent Capitalintent = new Intent(PersonActivity.this, CapitalActivity.class);
                startActivity(Capitalintent);
                break;
            case R.id.ar_discount:
                Intent YHJintent = new Intent(PersonActivity.this, YHJActivity.class);
                startActivity(YHJintent);
                break;
            case R.id.JFSC_Layout:
                Intent JFSCintent = new Intent(PersonActivity.this, JFSCActivity.class);
                startActivity(JFSCintent);
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (activityName.equals(getClass().getSimpleName())) {
                unreg();
                finish();
                break;
            }
        }
    }

    void unreg() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        getInformation();
    }

    void getInformation() {
        UserNetWorks.getInformation("", new Subscriber<InformationBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(InformationBean informationBean) {
                InformationBean.ReturnBodyBean returnBodyBean = informationBean.getReturn_body();
                if (informationBean.getReturn_code().equals("SUCCESS")) {
                    SharedPreferences sp = JUtils.getSharedPreference();
                    SharedPreferences.Editor spEd = sp.edit();
                    spEd.putString("User_name", returnBodyBean.getName());
                    spEd.putString("User_sex", returnBodyBean.getSex());
                    spEd.putString("User_Img", returnBodyBean.getHeadImg());
                    spEd.putString("User_Phone", returnBodyBean.getUserPhone());
                    String str = returnBodyBean.getHeadImg();
                    Glide.with(PersonActivity.this).load(str)
                            .placeholder(R.mipmap.logo)
                            .bitmapTransform(new CropCircleTransformation(PersonActivity.this))
                            .into(ivHeader);
                    tvUsername.setText(returnBodyBean.getName());
                    tvPhoneNumber.setText(returnBodyBean.getUserPhone());
                    YEText.setText(returnBodyBean.getBalance());
                    spEd.commit();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unreg();
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success极光推送别名设置成功";
                    Log.i("mAliasCallback", logs);
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败，60秒后重试";
                    Log.i("mAliasCallback", logs);
                    break;
                default:
                    logs = "极光推送设置失败，Failed with errorCode = " + code;
                    Log.e("mAliasCallback", logs);
                    break;
            }
        }
    };

}
